import ToolRequest from '@/base/newToolAxios';
import RequestOption from '@/base/RequestOption';
import { getActToolUrl } from '@/base/constant';
import dayjs from 'dayjs';
const tr = new ToolRequest();
//接口定义
//附件
interface Attachment {
  attachmentName: string; //附件名称
  attachmentUrl: any; //附件内容
}
export default class TaskDao {
  /**
   * 查找所有任务
   * @returns
   */
  async findTasksByCourseId(courseId: number) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findTasksByCourseId',
        courseId: courseId
      },
      ro
    );
  }
  /**
   * 查找所有发布的任务
   * @returns
   */
  async findAssignmentsByCourseId(courseId: number) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findAssignmentsByCourseId',
        courseId: courseId
      },
      ro
    );
  }
  /**
   * 创建任务
   *
   * @param {number} projectId - 项目ID
   * @param {string} taskName - 任务名称
   * @param {number} taskType - 任务类型
   * @param {number} courseId - 所属课程
   * @param {dayjs}   new Date()  - 创建时间
   * @param {number} creatorId - 创建者ID
   * @param {Attachment[]} attachmentList - 附件
   * @returns {Object} 任务列表 - 200表示创建成功，0表示创建失败
   */
  async createTask(formState: any) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'createTask',
        taskName: formState.taskName,
        projectId: formState.projectId,
        createTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        creatorId: formState.creatorId,
        taskDescription: formState.taskDescription,
        taskType: formState.taskType,
        courseId: formState.courseId,
        attachmentList: JSON.stringify(formState.attachmentList)
      },
      ro
    );
  }

  /**
   * 查看课程项目完成情况
   * @returns
   */
  async findTaskStatus(courseId: any) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findTaskStatus',
        courseId: courseId
      },
      ro
    );
  }

  /**
   * 发布任务
   * @returns
   */
  async addAssignment(formState: any) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'addAssignment',
        taskId: formState.taskId,
        team: formState.team.join(','),
        percentage: formState.precentage,
        endTime: formState.endTime.format(),
        creatorId: 1
      },
      ro
    );
  }

  /**
   * 跟新任务
   * @returns
   */
  async updateTask(formState: any) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'updateTask',
        taskId: formState.taskId,
        taskName: formState.taskName,
        deliverableList: JSON.stringify(formState.deliverableList)
      },
      ro
    );
  }

  /**
   * 根据项目list和创建人id找到对应课程下，所属项目的任务完成情况
   * @returns
   */
  async findTasksByProjects(creatorId: number, projectList: any) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'findTasksByProjects',
        creatorId: creatorId,
        projectList: JSON.stringify(projectList)
      },
      ro
    );
  }
  /**
   * 根据项目ID，任务名称（可选）、任务类型（可选）、所属课程ID（可选）获取项目所有相关任务
   * @param {number} projectId - 项目ID
   * @param {string} taskName - 任务名称
   * @param {number} taskType - 任务类型
   * @param {number} courseId - 所属课程
   * @param {number} creatorId - 创建人Id
   * @param {number} userId - 承担者Id
   * @param {number} currentPage - 当前页数
   * @param {number} pageSize   -每页条数
   * @returns {Object} 任务列表 - 200表示创建成功，0表示创建失败
   */
  async findTasksByProjectId(
    projectId: number,
    taskName: string,
    taskType: number,
    courseId: number,
    creatorId: number,
    userId: number,
    currentPage: number,
    pageSize: number
  ) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findTasksByProjectId',
        projectId: projectId,
        taskName: taskName,
        taskType: taskType,
        courseId: courseId,
        creatorId: creatorId,
        userId: userId,
        currentPage: currentPage,
        pageSize: pageSize
      },
      ro
    );
  }

  /**
   * 根据项目ID,返回该项目下所有任务的分配记录的状态数量。
   *
   * @param  projectId 项目ID
   * @param  evaluationType 评价类型
   * @return  返回记录状态数量的列表，依次为分配总数、已完成、进行中、未开始、延期、未批阅、完成率、延期率。
   */
  async findTaskAssignmentNumber(projectId: number, evaluationType: number) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findTaskAssignmentNumber',
        projectId: projectId,
        evaluationType: evaluationType
      },
      ro
    );
  }
  /**
   * 根据任务ID获取任务详情信息
   *
   * @param taskId 任务ID
   * @param projectType 任务ID
   * @return 返回一个携带任务基本信息的任务DTO实体类
   */
  async findTaskByTaskId(taskId: number, projectType: number) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findTaskByTaskId',
        taskId: taskId,
        projectType: projectType
      },
      ro
    );
  }
  /**
   * 修改任务名称
   *
   * @param taskId 任务ID
   * @param taskName 任务名称
   */
  async updateTaskName(taskId: number, taskName: string) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'updateTaskName',
        taskId: taskId,
        taskName: taskName
      },
      ro
    );
  }

  /**
   * 修改任务描述
   *
   * @param taskId 任务ID
   * @param taskDescription 任务描述
   */
  async updateTaskDescription(taskId: number, taskDescription: string) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'updateTaskDescription',
        taskId: taskId,
        taskDescription: taskDescription
      },
      ro
    );
  }

  /**
   *
   * 修改任务类型
   *
   * @param taskId 任务ID
   * @param taskType 任务类型
   */
  async updateTaskType(taskId: number, taskType: number) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'updateTaskType',
        taskId: taskId,
        taskType: taskType
      },
      ro
    );
  }

  /**
   *
   * 修改任务所属课程
   *
   * @param taskId 任务ID
   * @param courseId 课程ID
   */
  async updateTaskCourse(taskId: number, courseId: number) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'updateTaskCourse',
        taskId: taskId,
        courseId: courseId
      },
      ro
    );
  }

  /**
   *
   * 为任务新增分配记录和承担人
   *
   * @param taskId 任务ID
   * @param creatorId 创建者ID
   * @param createTime 创建时间
   * @param endTime 截止时间
   * @param userIdList 用户ID列表
   */
  async addAssignmentAndUndertaker(formState) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'addAssignmentAndUndertaker',
        taskId: formState.taskId,
        creatorId: formState.creatorId,
        createTime: formState.createTime.format('YYYY-MM-DD HH:mm:ss'),
        endTime: formState.endTime.format('YYYY-MM-DD HH:mm:ss'),
        userIdList: formState.userList.join(','),
        teamId: formState.teamId
      },
      ro
    );
  }
  /**
   * 查询某个任务下的所有分配记录
   *
   * @param taskId 任务ID
   * @param projectType 项目类型
   * return 返回分配记录列表
   */
  async findAssignmentsByTaskId(taskId, projectType) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findAssignmentsByTaskId',
        taskId: taskId,
        projectType: projectType
      },
      ro
    );
  }

  /**
   * 根据分配记录ID删除某个任务分配记录
   *
   * @param assignmentId 分配记录ID
   */
  async deleteAssignmentByAssignmentId(assignmentIdList) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'deleteAssignmentByAssignmentId',
        assignmentIdList: assignmentIdList.join(',')
      },
      ro
    );
  }

  /**
   * 更新单个分配记录的截止日期、分配小组和承担用户
   *@param assignmentId  要修改的记录id
   * @param endTime       截止日期
   * @param teamId        小组id
   * @param userIds       用户id列表
   */
  async updateAssignmentEndTimeAndMember(assignmentId, endTime, teamId, userIds) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'updateAssignmentEndTimeAndMember',
        assignmentId: assignmentId,
        endTime: endTime.format('YYYY-MM-DD HH:mm:ss'),
        teamId: teamId,
        userIds: userIds.join(',')
      },
      ro
    );
  }
  /**
   * 更新多个分配记录的截止日期
   *
   * @param assignmentIdList 分配记录id列表
   * @param endTime          截止日期
   */
  async updateAssignmentsEndTimeByAssignmentIds(assignmentIdList, endTime) {
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'updateAssignmentsEndTimeByAssignmentIds',
        assignmentIdList: assignmentIdList.join(','),
        endTime: endTime.format('YYYY-MM-DD HH:mm:ss')
      },
      ro
    );
  }

  /**
   * 根据项目ID,返回该项目下所有任务的分配记录。
   *
   * @param projectId 项目ID
   * @param assignmentStatus 分配记录状态
   * @param taskType 任务类型
   * @param courseId 课程ID
   * @param projectType 项目类型
   * @return 返回记录状态的列表。
   */
  async findAssignmentsByProjectId(projectId, assignmentStatus, taskType, courseId, projectType) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findAssignmentsByProjectId',
        projectId: projectId,
        assignmentStatus: assignmentStatus,
        taskType: taskType,
        courseId: courseId,
        projectType: projectType
      },
      ro
    );
  }

  async findSubmissionAndAchievementByTasksId(tasksId: Array<number>, projectType: number) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findSubmissionAndAchievementByTasksId',
        tasksId: tasksId.join(','),
        projectType: projectType
      },
      ro
    );
  }

  /**
   * 根据课程ID,返回该课程下关联的任务的分配记录的状态数量。
   *
   * @param courseId 课程ID
   * @param evaluationType 评价类型
   * @return 返回记录状态数量的列表，依次为分配总数、已完成、进行中、未开始、延期、未批阅。
   */
  async findTaskAssignmentNumberByCourseId(courseId: number, evaluationType: number) {
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findTaskAssignmentNumberByCourseId',
        courseId: courseId,
        evaluationType: evaluationType
      },
      ro
    );
  }
}
